package com.menchao.carsales.service.impl;


import com.github.pagehelper.PageHelper;
import com.menchao.carsales.common.http.Const;
import com.menchao.carsales.common.http.ResponseData;
import com.menchao.carsales.dao.EmployeeMapper;
import com.menchao.carsales.pojo.Employee;
import com.menchao.carsales.service.IEmployeeService;
import com.menchao.carsales.vo.EmployeeQuery;
import com.menchao.carsales.vo.ListPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerResponse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Porject: carsales
 * Date: 2021/7/22
 * Author: menchao
 * Description:
 */

@Service
public class EmployeeService implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public ResponseData addEmployee(Employee employee) {
        employee.setId(createEmployeeId());
        employee.setRole(Const.Number.ONE);
        System.out.println(employee.toString());
        int resultCount = employeeMapper.insert(employee);
        if (resultCount > 0) {
            return ResponseData.success();
        }
        return ResponseData.errorMessage("添加失败");
    }

    @Override
    public ResponseData getList(EmployeeQuery employeeQuery) {

        List<Employee>  list  = PageHelper.startPage(employeeQuery.getPage(),
                employeeQuery.getLimit()).doSelectPage(()-> employeeMapper.selectSelective(employeeQuery)
        );
        if (list != null){
            ListPage listPage = new ListPage();

            long totalSize =  PageHelper.count(()->employeeMapper.selectSelective(employeeQuery));

            System.out.println("getList totalSize: " + totalSize);

            listPage.setTotal(totalSize);
            listPage.setItems(list);
            listPage.setPageNumber(employeeQuery.getPage());
            listPage.setPageSize(employeeQuery.getLimit());
            return  ResponseData.success(listPage);
        }
        //获取总数
        return ResponseData.faile("获取用户列表失败");
    }


    @Override
    public ResponseData updateEmployee(Employee employee) {
        int resultCount = employeeMapper.updateByPrimaryKeySelective(employee);

        System.out.println("updateEmployee : " + (resultCount>0?"更新成功":"更新失败"));
        if (resultCount > 0) {
            return ResponseData.success();
        }
        return ResponseData.faile("更新雇员失败");
    }

    @Override
    public ResponseData deleteEmployee(Employee employee) {

        try {
            int resultCount = employeeMapper.delete(employee);
            if (resultCount > 0){
                return ResponseData.success();
            }
            return  ResponseData.faile("删除新雇员失败");
        }catch (Exception exception){
            System.out.println("deleteEmployee exception : " + exception.toString());
            return ResponseData.faile(exception.toString());
        }
    }

    @Override
    public ResponseData validPassword(Integer id, String validPass) {
        return null;
    }

    @Override
    public ResponseData updatePassword(Integer id, String oldPass, String newPass) {
        return null;
    }

    /**
     * 客户编号
     * 格式为：yyMM 加 三位递增的数字，数字每月重置为1
     * @return
     */
    private Integer createEmployeeId() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMDD");
        String format = dateFormat.format(new Date()) + "000";
        return Integer.valueOf(format) + (num++);
    }

    private int num = 1;
}
